package com.ww.microrpc.netty;

import com.ww.microrpc.client.Client;
import com.ww.microrpc.executer.impl.BeanMethodExecutors;
import com.ww.microrpc.executer.MethodExecutors;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RpcServiceAutoConfiguration {

    /**
     * register method executor
     */
    @Bean
    @ConditionalOnMissingBean
    public MethodExecutors getMethodExecutors(){
        return new BeanMethodExecutors();
    }

    /**
     * read properties
     */
    @Bean
    public MicroRpcProperties microRpcProperties(){
        return new MicroRpcProperties();
    }

    /**
     * netty server config
     */
    @Bean
    @ConditionalOnMissingBean
    public NettyServerConfig nettyServerConfig(MicroRpcProperties microRpcProperties) {
        return new NettyServerConfig(microRpcProperties);
    }

    /**
     * netty client config
     */
    @Bean
    @ConditionalOnMissingBean
    public NettyClientConfig nettyClientConfig(MicroRpcProperties microRpcProperties) {
        return new NettyClientConfig(microRpcProperties);
    }

    @Bean
    @ConditionalOnMissingBean
    public Client microRpcclient(NettyClientConfig nettyClientConfig) {
        return new NettyClient(nettyClientConfig);
    }

    /**
     * netty server
     */
    @Bean
    @ConditionalOnMissingBean
    public NettyServer getRpcService(NettyServerConfig nettyServerConfig, MethodExecutors methodExecutors) {
        return new NettyServer(methodExecutors, nettyServerConfig);
    }

}
